import turtle
import random

ms = turtle.Screen()
ms.setup(720, 550, 300, 100)
ms.register_shape('image/bc.gif')
ms.register_shape('image/bc3.gif')
ms.register_shape('image/bc4.gif')
ms.register_shape('image/bc5.gif')
ms.register_shape('image/bc6.gif')
ms.title('数字华容道')
ms.tracer(0)


class Number(turtle.Turtle):
    def __init__(self):
        super().__init__()
        self.penup()
        self.ht()

    def paint(self, x, y, n):
        a = int(400 / model - 4)
        b = int(a / 2)
        self.goto(x, y)
        self.color('black', '#999999')
        self.goto(x - b, y + b)
        self.pd()
        self.begin_fill()
        self.goto(x + b, y + b)
        self.goto(x + b, y - b)
        self.goto(x - b, y - b)
        self.goto(x - b, y + b)
        self.end_fill()
        self.penup()
        self.goto(x, y)
        self.color('white')
        if n > 0:
            self.write(f'{n}', align='center', font=('Arial', 15, 'bold'))

    def move(self, px, py):
        if refuse == 1:
            for i in range(model * model):
                j = dic[i]
                if (j.xcor() + px, j.ycor() + py) == dic[0].pos():
                    dic[0].paint(j.xcor(), j.ycor(), 0)
                    j.paint(j.xcor() + px, j.ycor() + py, i)
                    break

    def move_right(self):
        self.move(c, 0)
        game.judge()

    def move_left(self):
        self.move(-c, 0)
        game.judge()

    def move_up(self):
        self.move(0, c)
        game.judge()

    def move_down(self):
        self.move(0, -c)
        game.judge()


class Back(turtle.Turtle):
    def __init__(self):
        super().__init__()
        self.penup()
        self.ht()

    def draw(self, x, y, px, py, text):
        self.color('#444444', '#aaaaaa')
        self.goto(x, y)
        self.pd()
        self.begin_fill()
        self.goto(x, y + py)
        self.goto(x - px, y + py)
        self.goto(x - px, y)
        self.goto(x, y)
        self.end_fill()
        self.pu()
        self.goto(x - px / 2, y + py / 4)
        self.write(f'{text}', align='center', font=('黑体', 15, 'bold'))

    def show_win(self):
        self.color('blue')
        self.goto(0 + sister, 10)
        self.write('恭喜，VICTORY', align='center', font=('黑体', 30, 'bold'))
        ms.bgpic(f'bc{model}.gif')

    def text_unlock(self, text):
        text_unlock.clear()
        self.color('#444444')
        self.goto(-195 + sister, 166)
        self.write('通过本关', align='left', font=('黑体', 15, 'bold'))
        self.goto(-195 + sister, 140)
        self.write(f'{text}', align='left', font=('黑体', 15, 'bold'))


class Game():
    def init(self):
        global dic, pos, c, model, num_block_list
        ms.bgpic('image/bc.gif')
        dic, pos = {}, []
        for i in num_block_list:
            i.clear()
        n = 0
        a = int(200 - 200 / model)
        b = int(265 - 200 / model)
        c = int(400 / model)
        for i in range(model):
            for j in range(model):
                x = a - j * c + sister
                y = -b + i * c
                num_block = Number()
                num_block.paint(x, y, n)
                dic[n] = num_block
                pos.append((x, y))
                n = n + 1
                num_block_list.append(num_block)

    def judge(self):
        global refuse
        if refuse == 1:
            for i in range(model * model):
                if dic[i].pos() == pos[i]:
                    continue
                else:
                    return
            win_text.show_win()
            refuse = 0

    def mouse_choose(self, x, y):
        global model, init_list, refuse
        win_text.clear()
        if 100 + sister < x < 200 + sister and 215 < y < 265:  # 开始游戏
            init_list = []  # 打乱顺序的列表，为重玩本局准备
            refuse = 1
            for i in range(10):
                j = random.choice([(c, 0), (-c, 0), (0, c), (0, -c)])
                init_list.append(j)
                num_block.move(j[0], j[1])
        if 100 + sister < x < 200 + sister and 155 < y < 205:  # 重玩本局
            game.init()
            refuse = 1
            for k in init_list:
                num_block.move(k[0], k[1])
        if -195 + sister < x < -105 + sister and 232 < y < 265:  # 3x3
            model = 3
            game.init()
            text_unlock.text_unlock('解锁旗木卡卡西')
        if -195 + sister < x < -105 + sister and 194 < y < 227:  # 4x4
            model = 4
            game.init()
            text_unlock.text_unlock('解锁宇智波佐助')
        if -95 + sister < x < -5 + sister and 232 < y < 265:  # 5x5
            model = 5
            game.init()
            text_unlock.text_unlock('解锁我爱罗')
        if -95 + sister < x < -5 + sister and 194 < y < 227:  # 6x6
            model = 6
            game.init()
            text_unlock.text_unlock('解锁宇智波鼬')


init_list = []
refuse = 0
sister = 150
model = 3
num_block_list = []
game = Game()
game.init()
num_block = Number()
win_text = Back()
back_block = Back()
text_unlock = Back()
text_unlock.text_unlock('解锁旗木卡卡西')
back_block.draw(200 + sister, 215, 100, 50, '开始游戏')
back_block.draw(200 + sister, 155, 100, 50, '重玩当局')
back_block.draw(-105 + sister, 232, 90, 33, '3x3')
back_block.draw(-105 + sister, 194, 90, 33, '4x4')
back_block.draw(-5 + sister, 232, 90, 33, '5x5')
back_block.draw(-5 + sister, 194, 90, 33, '6x6')

ms.listen()
ms.onkey(num_block.move_right, 'Right')
ms.onkey(num_block.move_left, 'Left')
ms.onkey(num_block.move_up, 'Up')
ms.onkey(num_block.move_down, 'Down')
ms.onclick(game.mouse_choose)

ms.mainloop()
